package com.demo.springbootunpack.controller;

import com.demo.springbootunpack.service.IisArchiveFile;
import com.demo.springbootunpack.utils.UnPackUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 * @author Hanson
 * @date 2022/9/19 14:29
 */
@RestController
@RequestMapping(value = "/zip")
public class UnPackController {

    @Resource
    private UnPackUtil unPackUtil;

    @Resource
    private IisArchiveFile iisArchiveFile;
    private final static String ZIP_FILE = "application/zip";
    private final static String RAR_FILE = "application/vnd.rar";

    @PostMapping("/unzip")
    public void  uploadPack(MultipartFile uploadFile) {

        boolean isZipPack = true;

        // 解压目录
        String packFileStr = "D:\\zip";

        //不存在则创建
        File packFile = new File(packFileStr);
        if (!packFile.exists()) {
            boolean mkdirs = packFile.mkdirs();
        }

        if (uploadFile== null) {
            throw new RuntimeException("请上传文件");
        }
        String contentType = uploadFile.getContentType();
        String filename = uploadFile.getOriginalFilename();
        //将压缩包保存在指定路径
        String packFilePath = packFileStr + File.separator + filename;
        if (ZIP_FILE.equals(contentType)) {
            //zip解压缩处理
        } else if (RAR_FILE.equals(contentType)) {
            //rar解压缩处理
            isZipPack = false;
        } else {
            throw new RuntimeException("上传的压缩包格式不正确,仅支持rar和zip压缩文件!");
        }

        File file = new File(packFilePath);

        try {
            uploadFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("压缩文件到:" + packFileStr + " 失败!");
        }
        if (isZipPack) {
            //zip压缩包
            unPackUtil.unPackZip(file, null, packFileStr);
        } else {
            //rar压缩包
            unPackUtil.unPackRar(file, packFileStr);
        }
        //获取压缩包名称
        filename = filename.substring(0, filename.lastIndexOf("."));

        System.out.println("解压成功，解压目录：" + packFilePath);
        //可以根据解压路径、压缩包名称、文件名称，取出对应文件进行操作
    }

    /**
     * 判断文件是不是zip格式   rar也返回false
     * @param file
     * @return
     */
    @PostMapping("/isArchiveFile")
    public boolean isArchiveFile(MultipartFile file){
        File upgradeFile = transferToFile(file);
        boolean archiveFile = iisArchiveFile.isArchiveFile(upgradeFile);
        return archiveFile;
    }

    /**
     * 将MultiPartFIle文件转换成File文件
     *
     * @param multipartFile multipartFile文件
     * @return file文件
     */
    private File transferToFile(MultipartFile multipartFile) {
//        选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            file = File.createTempFile(filename[0], filename[1]);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
